package com.bby.user.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.MD5;
import com.bby.common.exception.BBYException;
import com.bby.common.utils.BBYUtils;
import com.bby.common.utils.R;
import com.bby.common.utils.StatusCode;
import com.bby.common.aop.ParseToken;
import com.bby.user.bean.req.EditPasswdReq;
import com.bby.user.bean.req.MailEditReq;
import com.bby.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * 用户-修改/获取用户信息
 */
@RestController
@RequestMapping("info")
public class UserInfoController {

    private final UserService userService;
    Logger logger = LoggerFactory.getLogger(this.getClass());

    public UserInfoController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 获取用户公共信息
     * @return json数据
     */
    @ResponseBody
    @GetMapping("pubInfo/{userId}")
    public R getUserPubInfo(@PathVariable String userId) throws BBYException {
        if (ObjectUtil.isEmpty(userId)) {
            return R.error("参数错误");
        }
        return R.ok(userService.getUserPublicInfoById(userId));
    }

    /**
     * 获取用户信息
     */
    @ParseToken(require = true)
    @ResponseBody
    @GetMapping("userinfo")
    public R getUserInfo(String userId) throws BBYException {
        return R.ok(userService.getUserInfo(userId));
    }

    /**
     * 发送验证码到邮箱
     * @param mail 要发送的邮箱，为空则发给用户userId的邮箱
     */
    @GetMapping(value = "mailCaptcha")
    @ResponseBody
    @ParseToken(require = true)
    public R mailCaptcha(String userId, String mail) throws BBYException {
        userService.sendMilCaptcha(userId,mail);
        return R.ok();
    }

    /**
     * 获取邮箱验证码 token
     * @param value 验证码
     * @param mail 要发送的邮箱，为空则发给用户userId的邮箱
     */
    @GetMapping(value = "mailToken")
    @ResponseBody
    @ParseToken(require = true)
    public R mailToken(String userId,String mail, String value) throws BBYException {
        BBYUtils.checkEmpty(value);
        return userService.createMailToken(userId,mail, value);
    }

    /**
     * 修改邮箱
     *
     * @param params
     */
    @ResponseBody
    @ParseToken(require = true)
    @PostMapping( "edit/mail")
    public R editMail(String userId, @RequestBody MailEditReq params) throws BBYException {
        userService.editMail(userId,params);
        return R.ok();
    }
    /**
     * 修改密码
     */
    @PostMapping(value = "edit/passwd")
    @ParseToken(require = true)
    public R editPasswd(String userId, @RequestBody EditPasswdReq param) throws BBYException {
        BBYUtils.checkEmpty(param.getNewPw(),param.getCurrentPw());
        if (param.getNewPw().equals(param.getCurrentPw())){
            return R.error(StatusCode.INPUT_ERROR,"密码未改变！");
        }
        MD5 md5= MD5.create();
        param.setNewPw(md5.digestHex(param.getNewPw()));
        param.setCurrentPw(md5.digestHex(param.getCurrentPw()));
        userService.editPasswd(userId,param);
        return R.ok();
    }
    /**
     * 修改用户名, 用url参数
     */
    @PostMapping(value = "edit/custom/{key}")
    @ParseToken(require = true)
    public R editSignature(String userId,@PathVariable String key, String value) throws BBYException {
        BBYUtils.checkEmpty(key);
        userService.editCustom(userId,key,value);
        return R.ok();
    }
    /**
     * 修改用户名, 用url参数
     */
    @PostMapping(value = "edit/username")
    @ParseToken(require = true)
    public R editUsername(String userId, String username) throws BBYException {
        BBYUtils.checkEmpty(username);
        userService.editUsername(userId,username);
        return R.ok();
    }
    /**
     * 检查用户名是否可用
     * @param username 用户名
     */
    @GetMapping(value = "check/name/{username}")
    @ResponseBody
    public R checkName(@PathVariable("username") String username) throws BBYException {
        logger.debug("username = " + username);
        userService.checkName(username);
        return R.ok();
    }
    /**
     * 检查邮箱是否存在
     * @param mail 邮箱
     */
    @GetMapping(value = "check/mail/{mail}")
    @ResponseBody
    public R checkEmail(@PathVariable("mail") String mail) throws BBYException {
        logger.debug("检查邮箱是否存在: " + mail);
        userService.checkEmail(mail);
        return R.ok();
    }
}
